<template>
    <el-card class="box-card">
        <div slot="header" class="clearfix">
            <span>发起工单</span>
        </div>
        <el-col :span="18" :offset="3">
            <div class="form-conf" v-if="formOpen">
                <parser v-if="formData.formRef" :key="new Date().getTime()" :form-conf="formData" :isDetail="false"
                    @submit="submit" @save="saveForm" @cancel="cancelForm" ref="parser" @getData="getData" />
                <CptSelfForm v-else :formObj="formData"></CptSelfForm>
            </div>
        </el-col>
    </el-card>
</template>
  
<script>
import { getProcessForm, startProcess, startSave } from '@/api/workflow/process'
import { complete, instanceCancel } from '@/api/workflow/task'
import Parser from '@/utils/generator/parser'
import CptSelfForm from '@/components/CptSelfForm'


export default {
    name: 'SubStartCpt',
    components: {
        Parser,
        CptSelfForm
    },
    data() {
        return {
            definitionId: null,
            deployId: null,
            procInsId: null,
            formOpen: false,
            formData: {},
            taskId: null
        }
    },
    created() {
        this.initData();
    },
    methods: {
        initData() {
            this.deployId = this.$route.query && this.$route.query.deployId;
            this.definitionId = this.$route.query && this.$route.query.definitionId;
            this.procInsId = this.$route.query && this.$route.query.procInsId;
            this.taskId = this.$route.query && this.$route.query.taskId
            getProcessForm({
                definitionId: this.definitionId,
                deployId: this.deployId,
                procInsId: this.procInsId
            }).then(res => {
                if (res.data) {
                    this.formData = res.data;
                    this.formOpen = true
                }
            })
        },
        /** 接收子组件传的值 */
        getData(data) {
            if (data) {
                const variables = [];
                data.fields.forEach(item => {
                    let variableData = {};
                    variableData.label = item.__config__.label
                    // 表单值为多个选项时
                    if (item.__config__.defaultValue instanceof Array) {
                        const array = [];
                        item.__config__.defaultValue.forEach(val => {
                            array.push(val)
                        })
                        variableData.val = array;
                    } else {
                        variableData.val = item.__config__.defaultValue
                    }
                    variables.push(variableData)
                })
                this.variables = variables;
            }
        },
        submit(data) {
            if (data && this.definitionId) {
                this.inflateValData(data)
                // 启动流程并将表单数据加入流程变量
                // startProcess(this.definitionId, JSON.stringify(data.valData)).then(res => {
                //   this.$modal.msgSuccess(res.msg);
                //   this.$tab.closeOpenPage({
                //     path: '/work/own'
                //   })
                // })
                let taskForm = {
                    comment: "   ", // 意见内容
                    procInsId: this.procInsId, // 流程实例编号
                    taskId: this.taskId,// 流程任务编号
                    copyUserIds: "", // 抄送人Id
                    vars: "",
                    targetKey: "",
                    variables: data.valData
                }
                complete(taskForm).then(response => {
                    this.$modal.msgSuccess(response.msg);
                    this.$tab.closeOpenPage({
                        path: '/work/own'
                    })
                }).catch(error => {
                    console.error(error)
                })
            }
        },
        /**
         * 发起审批，提交用户信息数据
         * @author gaogm
         */
        inflateValData(data) {
            if (data && data.valData) {
                let user = this.$store.state.user
                let role = '';
                if (user && user.roles) {
                    role = user.roles[0]
                }
                data.valData.curRoleCode = role
            }
        },
        // 暂存
        saveForm(data) {
            if (data && this.taskId) {
                let taskForm = {
                    comment: "   ", // 意见内容
                    procInsId: this.procInsId, // 流程实例编号
                    taskId: this.taskId,// 流程任务编号
                    copyUserIds: "", // 抄送人Id
                    vars: "",
                    targetKey: "",
                    variables: data.valData
                }
                startSave(taskForm).then(response => {
                    this.$modal.msgSuccess(response.msg);
                    this.$tab.closeOpenPage({
                        path: '/work/own'
                    })
                }).catch(error => {
                    console.error(error)
                })
            }
        },
        cancelForm(data) {
            if (data && this.taskId) {
                let taskForm = {
                    comment: "   ", // 意见内容
                    procInsId: this.procInsId, // 流程实例编号
                    taskId: this.taskId,// 流程任务编号
                    copyUserIds: "", // 抄送人Id
                    vars: "",
                    targetKey: "",
                    variables: data.valData
                }
                instanceCancel(taskForm).then(res => {
                    this.$modal.msgSuccess(res.msg);
                    this.$tab.closeOpenPage({
                        path: '/work/own'
                    })
                });
            }
        },
    }
}
</script>
  
<style lang="scss" scoped>
.form-conf {
    margin: 15px auto;
    width: 100%;
    padding: 15px;
}
</style>